function [volume, flow]=calculate_volume_flow_fit_toolbox(bellows_volt, patient);
        %|------------------------------------------------------------------------|
    %|Calculation of flow from the bellows_voltage.
    %|  Smooths the bellows voltage using smooth, then calculates the
    %|  derivative of this waveform, using the patient.scan_rate to
    %|  calculate flow, and patient.direction to take into account the varying scan directions with time.
    %|  Smooths the results.
    %|  *Bellows volt is called as a separate variable, so flow can be
    %   calculated later also.
    %------------------------------------------------------------------------
    %|      Dependancies;                                                     |
    %|            choose_patient (finds data folders)                         |
    %------------------------------------------------------------------------
    %   This file is part of the
    %   5D-Novel4DCT Toolbox  ("Novel4DCT-Toolbox")
    %   DH Thomas, Ph.D
    %   University of California, Los Angeles
    %   Contact: mailto:dhthomas@mednet.ucla.edu
    %------------------------------------------------------------------------
    % $Author: DHThomas $	$Date: 2014/04/01 10:23:59 $	$Revision: 0.1 $

    flow=NaN(size(bellows_volt));
     for i = 1:patient.scans;
        volume(:,i) = smooth(bellows_volt(:,i),5);
    end
    for i=1:size(bellows_volt,2);
        bellows_volt_smoothed(:,i) =smooth(bellows_volt(:,i),25);%sgolayfilt(bellows_volt(:,i),7,15);
        % ind = 1:length(bellows_volt_smoothed(:,i));
        % k = ~isnan(bellows_volt_smoothed(:,i));
        %     fitcoeff  = polyfit(ind(k)',bellows_volt(:,i),7);
        vol_fit(:,i) = bellows_volt_smoothed(:,i);%polyval(fitcoeff,ind);%bellows_volt(:,i);% %
        
        
        for j=2:size(vol_fit,1)-1
            if (vol_fit(j+1,i)-vol_fit(j,i))~=0
                flow(j,i)=(vol_fit(j+1,i)-vol_fit(j,i))/patient.scan_rate;
            else
                flow(j,i)=flow(j-1,i);
            end
        end
        
        flow_tmp(:,i)=smooth(flow(:,i),5);
        flow_fit=flow_tmp;
    end
    
    for i=1:patient.scans;
        if patient.directions(i)<1;
            flow_fit(:,i)=-flow_fit(:,i);
        end
    end
    
   
    
%     volume=vol_fit;
    flow=flow_fit;
    
   
    
